package me.algo.array;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.PriorityQueue;

/**
 * @author guozheng
 * @date 2024/5/13
 */
public class A10 {




    class KthLargest {

        PriorityQueue<Integer> queue;

        int k;

        public KthLargest(int k, int[] nums) {
            this.queue = new PriorityQueue<>(k);
            this.k = k;
            for (int num : nums) {
                add(num);
            }
        }

        public int add(int val) {
            if (queue.size() == k) {
                Integer peek = queue.peek();
                if (val > peek) {
                    queue.remove();
                    queue.add(val);
                }
            } else {
                queue.add(val);
            }
            return queue.peek();
        }
    }
    public static int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<>(k);
        for (int num : nums) {
            if (queue.size() == k) {
                Integer peek = queue.peek();
                if (peek < num) {
                    queue.remove();
                    queue.add(num);
                }
                continue;
            }
            queue.add(num);
        }
        return queue.remove();
    }

    public static String kthLargestNumber(String[] nums, int k) {
        PriorityQueue<String> queue = new PriorityQueue<>(k, A10::compareStr);
        for (String s : nums) {
            if (queue.size() == k) {
                String last = queue.peek();
                if (compareStr(s, last) > 0) {
                    queue.remove();
                    queue.add(s);
                }
            } else {
                queue.add(s);
            }
        }
        return queue.peek();
    }

    private static int compareStr(String o1, String o2) {
        if (o1.length() == o2.length()) {
            if (o1.equals(o2)) {
                return 0;
            }
            return o1.compareTo(o2);
        } else if (o1.length() < o2.length()) {
            return -1;
        } else {
            return 1;
        }
    }

    public static void main(String[] args) {
        System.out.println(compareStr("1", "2"));
        System.out.println(compareStr("10", "2"));
        System.out.println(compareStr("11", "2"));
        System.out.println(compareStr("2", "2"));
        System.out.println(compareStr("2", "2000"));
    }


}
